<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.css" rel="stylesheet" />
		<link href="../css/header.css" rel="stylesheet" />
	</head>

	<body>
		<!-- 头部 -->
		<header class="mui-bar mui-bar-nav title">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color:white;"></a>
			<h1 class="mui-title title-color">详细资料</h1>
		</header>
		<div class="mui-content">
			<ul class="mui-table-view">
				<li class="mui-table-view-cell mui-media">
					<a href="javascript:;">
						<img id="img_friend_face" class="mui-media-object mui-pull-left" src="" style="border-radius: 5%; line-height: 50px;max-width: 50px;height: 50px;">
						<div class="mui-media-body">
							<label id="lab_friend_nickname"></label>
							<p id="p_friend_username" class="mui-ellipsis"></p>
						</div>
					</a>
				</li>
			</ul>
			<br />
			<div style="text-align: center;">
				<button id="btn_add_friend" type="button" class="mui-btn mui-btn-block" style=" background-color:#007AFF;color: white;width: 95%;display: inline;padding: 8px 0;">添加好友</button>
			</div>
		</div>

		<script src="../js/mui.js"></script>
		<script src="../js/app.js"></script>
		<script type="text/javascript">
			mui.init({
				swipeBack: true //启用右滑关闭功能
			});
			mui.plusReady(function() {
				//获取登录用户的全局信息
				var user = app.getUserGlobalInfo();
				//获取上一个页面传入的对象，并且对本页面相应的dom赋值初始化
				var currentWebview = plus.webview.currentWebview();
				//获取传递参数
				var friend = currentWebview.friend;
				console.log(JSON.stringify(friend));

				//获取头像对象
				var img_friend_face = document.getElementById("img_friend_face");
				var lab_friend_nickname = document.getElementById("lab_friend_nickname");
				var p_friend_username = document.getElementById("p_friend_username");
				var imgURL = friend.faceImage.substring(0, 4) === "http" ? friend.faceImage : app.imgServerUrl + friend.faceImage;
				img_friend_face.src = imgURL;
				lab_friend_nickname.innerHTML = friend.nickname;
				p_friend_username.innerHTML = "微涵号：" + friend.username;

				//发送添加好友请求
				var btn_add_friend = document.getElementById("btn_add_friend");
				btn_add_friend.addEventListener("tap", function() {
					plus.nativeUI.showWaiting("请稍后");
					mui.ajax(app.serverUrl + '/user/addUser', {
						//使用之前所存入的ip来进行通信
						data: {
							sendUserId: app.getUserGlobalInfo().id,
							acceptUserId: friend.id
						},
						type: 'post', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						success: function(data) {
							if (data.status == 200) {
								app.showToast(data.data, "succeed");
								plus.nativeUI.closeWaiting();
								console.log(JSON.stringify(data));
							} else { //服务器返回的数据为空,表明账户与密码验证失败
								app.showToast(data.data, "error"); //弹出错误信息
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {
							if (textStatus == "timeout") {
								mui.toast("连接超时"); //超过十秒报错
							} else if (textStatus == "error") {
								mui.toast("连接出错");
							}
						}
					});

				})
			});
		</script>
	</body>

</html>
